package com.easygem.front.service.imp;

import com.easygem.common.model.system.SysInformation;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.vo.system.SysInformationVO;
import com.easygem.front.mapper.SysInformationMapper;
import com.easygem.front.service.SysInformationService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 李鹏志
 * @version 1.0
 * @Description 系统信息业务逻辑层实现类
 * @date 2025/1/11 16:19
 */
@Service
public class SysInformationServiceImpl  implements SysInformationService {
    @Autowired
    private SysInformationMapper sysInformationMapper;
    /**
     * 获取系统信息
     *
     * @return 系统信息
     */
    @Override
    public SysInformation getSysInformation(SysInformationVO sysInformationVO) {
        //获取用户账户名
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        SysInformation sysInformation = new SysInformation();
        sysInformation.setAccountName(activeUser.getUser().getAccountName());
        SysInformation sysInformation1 = sysInformationMapper.getSysInformation(sysInformation);
        System.out.println("++++++++++++++++++++++++++="+sysInformation1);
        return  sysInformation1;
    }

    /**
     * 修改系统信息
     *
     * @param sysInformationVO 系统信息
     * @return 影响行数
     */
    @Override
    @Transactional
    public String updateSysInformation(SysInformationVO sysInformationVO) {
        //获取用户角色
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        //判断用户是否为管理员
        if (activeUser.getUser().getType()!=0) {
            return "noPermission";
        };
        SysInformation sysInformation = new SysInformation();
        sysInformation.setInfoId(sysInformationVO.getInfoId());
        sysInformation.setHighSeasCount(sysInformationVO.getHighSeasCount());
        sysInformation.setHighSeasTime(sysInformationVO.getHighSeasTime());
        sysInformation.setExpiryDay(sysInformationVO.getExpiryDay());
        sysInformation.setIsEnabledToHighSeas(sysInformationVO.getIsEnabledToHighSeas());
        sysInformation.setRemark(sysInformationVO.getRemark());
        sysInformation.setAccountName(activeUser.getUser().getAccountName());
        int count = sysInformationMapper.updateSysInformation(sysInformation);
        if (count > 0){
            return "success";
        }else {
            return "error";
        }
    }
}
